package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import com.abc.robot01.activity.MYBLACK_Percent5
import com.abc.robot01.activity.MyButton01
import com.abc.robot01.activity.MyFont_Size
import com.abc.robot01.activity.TwoStateButton

class PopupDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val  names= listOf("木质","陶瓷","金属","石材")
        val  names2= listOf("简约","舒适","豪华")
        setContent {
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
            ) {
                BasicPopupDemo(names,names2)
//                PopupDemo2()
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
            }
        }
    }
}

fun clicked(tag:String){



}

@Composable
fun BasicPopupDemo(list: List<String>, list2: List<String>) {
    val showPopup = remember { mutableStateOf(false) }
    val selected1 = remember { mutableStateOf("不限") }
    val selected2 = remember { mutableStateOf("不限") }
    val names = remember { list }
    val names2 = remember { list2 }

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0x00667788))
    ) {
        Button(onClick = { showPopup.value = true }) {
            Text(text = "Show Popup")
        }
        if (showPopup.value) {
            Popup(
                alignment= Alignment.TopStart,
                offset =  IntOffset(1*30, 1*160),
                onDismissRequest = { showPopup.value = false }) {
                Card(modifier = Modifier.size( (1026).dp/3.5f,(232).dp/3.5f)) {
                    Column(modifier = Modifier
                        .fillMaxWidth()
//                        .background(Color.Red)
                        .offset(y = 10.dp, x = 10.dp))
                             {
                        Row(verticalAlignment=Alignment.CenterVertically) {
                            Text(text = "适合喜欢", fontSize = MyFont_Size)
                            names2.forEach {
                                Spacer(modifier = Modifier.width(6.dp))
                                TwoStateButton(selected1,"$it" , onClick = ::clicked)
                            }
                        }
                        Spacer(modifier = Modifier.height(4.dp))
                        Row(verticalAlignment=Alignment.CenterVertically) {
                            Spacer(modifier = Modifier.width(14.dp))
                            Text(text = "材料", fontSize = MyFont_Size)
                            names.forEach {
                                Spacer(modifier = Modifier.width(6.dp))
                                TwoStateButton(selected2,"$it",  onClick = ::clicked)
                            }
                        }

                                 Row(verticalAlignment=Alignment.CenterVertically,
                                     modifier = Modifier
                                         .fillMaxWidth().offset(y=10.dp)
                                         .wrapContentWidth(Alignment.CenterHorizontally)
                                     ) {
                                     MyButton01(text = "重置",containerColor= MYBLACK_Percent5,textColor=Color.Black){
                                         selected1.value=""
                                         selected2.value=""
                                     }
                                     Spacer(modifier = Modifier.width(20.dp))
                                     MyButton01(text = "确认",containerColor=Color.Blue,textColor=Color.White){
                                         showPopup.value = false
                                     }
                                 }
                             }
                }
            }
        }
    }
}

@Composable
fun BasicPopupDemo02() {
    val showPopup = remember { mutableStateOf(false) }

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .height(300.dp)
            .fillMaxWidth()
            .background(Color(0xff667788))
    ) {
        Button(onClick = { showPopup.value = true }) {
            Text(text = "Show Popup")
        }

        if (showPopup.value) {
            Popup(
                alignment= Alignment.Center,
                onDismissRequest = { showPopup.value = false }) {
                Box(
                    modifier = Modifier
                        .padding(20.dp)
                        .padding(50.dp)
                        .clickable { showPopup.value = false }
                ) {
                    Text(text = "This is a basic popup")
                }
            }
        }
    }
}







